/* $RCSFile: ADNCRMVersion.ascx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/ADNCRMVersion/ADNCRMVersion.ascx.cs $
 * $log$
 * Revision 20 2010/08/09 10:51:18  christian.surieux
 *   Added Log comment to all .aspx .ascx .master files
 * Revision 19 2010/08/09 09:43:44  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:24  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Security;
using System.IO;
using System.Globalization;

using AddonNice.Diagnostics;
using AddonNice.Helpers;
using AddonNice.Settings;
using AddonNice.Security;
using AddonNice.Configuration;
using AddonNice.Configuration.Settings;
using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.UI.DataTypes;

using Trace2    =   System.Diagnostics.Trace;
using Path2    =   AddonNice.Settings.Path;


namespace AddonNice.PageModules.ADNCRMVersion
{
    public class AStr
    {
        public const string VersionDebug    =   "VERSION_DEBUG";
        public const string VersionLogo1    =   "VERSION_LOGO_IMAGE_URL";
        public const string VersionLogo2    =   "VERSION_LOGO_IMAGE2_URL";
    }

    /// <summary>
    /// Initializer class for ADNCRMVersion 
    /// </summary>
    public class ADNCRMVersionSI : PortalModuleControlSI
    {

        public ADNCRMVersionSI(ModuleSettingsDictionary bSettings,ISettingsOwner pm) : base()
        {
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]ADNCRMVersionSI Ctor BEG.",ModuleTraceSwitch.Sw.Info);
            InitSettings(bSettings,pm);
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]ADNCRMVersionSI Ctor END.",ModuleTraceSwitch.Sw.Info);
        }
 
        public override void InitSettings(ModuleSettingsDictionary bSettings,ISettingsOwner pm)
        {             
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]ADNCRMVersionSI InitSettings BEG.",ModuleTraceSwitch.Sw.Info);
            
            base.InitSettings(bSettings,pm);
            string extgrp               =   pm.ExtendedResourcesGroup;
			
            SettingItemSpecial src      =   new SettingItemSpecial(AStr.VersionLogo1,
                                                new UploadedFileDataType2()); //PortalUrlDataType
			src.Required                =   false;
			src.Order                   =   1;
            src.ExtendedGroupName       =   extgrp;
            src.Description             =   "Url for logo image";
            src.EnglishName             =   "Logo url";
            src.Value                   =   "adn_AddonniceLogo.png";
			bSettings.AddSetting(src);

			SettingItemSpecial src2     =  new SettingItemSpecial(AStr.VersionLogo2,
                                                new UploadedFileDataType2()); //PortalUrlDataType
			src2.Required               =  false;
			src2.Order                  =  2;
            src2.ExtendedGroupName      =  extgrp;
            src2.Description            =  "Url for second image";
            src2.EnglishName            =  "Second Logo url";
            src2.Value                  =  "adn_AddonniceLogo.png";
			bSettings.AddSetting(src2);

			SettingItemSpecial Dbg      =  new SettingItemSpecial(AStr.VersionDebug,new BooleanDataType());
			Dbg.Order                   =  5;
            Dbg.ExtendedGroupName       =  extgrp;
            Dbg.Description             =  "Detailled mode";
            Dbg.EnglishName             =  "Displayed detailled AddonNice info";
            Dbg.Value                   =  StStr.strFalse;
			bSettings.AddSetting(Dbg);
        }
    }
	
    /// <summary>
	/// Module used to display Portal versions
	/// </summary>
	public partial class ADNCRMVersion : PortalModuleControl
	{
		public override bool AdminModule
		{
			get
			{
				return true;
			}
		}

		public ADNCRMVersion() 
		{}

        public override string ExtendedResourcesGroup 
		{
            get
            {
                const string extgrp         =   "ADNCRMVERSION";
                return extgrp;
            }
		}
 
        public override void InitPortalModuleControl(ModuleSettingsDictionary bSettings)
        {
            PortalModuleInitializer =   new ADNCRMVersionSI(bSettings,this);
        }

        private void ADNCRMVersion_Load(object sender, System.EventArgs e)
		{
			makeTable();
		}

		void addBr(HtmlTableCell c)
		{
			Literal lit = new Literal();
			lit.Text = "<br>";
			c.Controls.Add(lit);
		}

		/// <summary>
		/// Full Assembly for Core assembly
		/// </summary>
		public static FileVersionInfo AddonNiceAssemblyInfo
		{
			get
			{
                string flName                           =   System.Reflection.Assembly.GetExecutingAssembly().Location;
                if ( SettingsTraceSwitch.Sw.TraceVerbose )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNCRMVersion AddonNiceAssemblyInfo: {0}", flName),SettingsTraceSwitch.Sw.Info);
                return FileVersionInfo.GetVersionInfo(flName);
			}
		}

        void makeTable()
		{
            bool VersionDebug       =   Settings[AStr.VersionDebug].ToBool();
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine(string.Format(CultureInfo.InvariantCulture,"[V]ADNCRMVersion makeTable BEG VersionDebug: {0}.",VersionDebug),ModuleTraceSwitch.Sw.Info);
			HtmlTable tb			=	new HtmlTable();
			tb.CellPadding			=	0;
			tb.CellSpacing			=	0;
			tb.Border				=	0;
			tb.Width				=	"100%";
			//  we adapt format, if version in middle: 2 columns, else 1 column
			bool oneCol				=	true;
			if ( ModuleBaseConfiguration.PaneName.ToLower() == "contentpane" )
				oneCol				=	false;
			
			// First row ( or 2 first )
			HtmlTableRow rw			=	new HtmlTableRow();
            rw.Style.Add( HtmlTextWriterStyle.Width,"100%");
			HtmlTableCell c			=	 new HtmlTableCell();
			c.Align					=	"center";
			c.Width					=	oneCol ? "100%":"50%";
			ImageButton btn			=	new ImageButton();
			btn.ID					=   "ImageButton1" ;
			btn.ImageUrl			=	Settings[AStr.VersionLogo1].FullPath;
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNCRMVersion makeTable btn.ImageUrl: {0}.",btn.ImageUrl),ModuleTraceSwitch.Sw.Info);
			c.Controls.Add(btn);
			rw.Controls.Add(c);
			if ( oneCol )
			{
				tb.Controls.Add(rw);
				rw					=	new HtmlTableRow();
                rw.Style.Add( HtmlTextWriterStyle.Width,"100%");
			}
			c						=	 new HtmlTableCell();
			c.Align					=	"center";
			c.Width					=	oneCol ? "100%":"50%";
			btn						=	new ImageButton();
			btn.ID					=	"ImageButton2" ;
			btn.ImageUrl			=	Settings[AStr.VersionLogo2].FullPath;
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNCRMVersion makeTable btn2.ImageUrl: {0}.",btn.ImageUrl),ModuleTraceSwitch.Sw.Info);
			c.Controls.Add(btn);
			rw.Controls.Add(c);
			// second image on bottom id only one col
			HtmlTableRow rw1		= rw;
			if ( !oneCol )
				tb.Controls.Add(rw);
			
			// Second 
			rw						=	new HtmlTableRow();
			rw.Style.Add( HtmlTextWriterStyle.Width,"100%");
			c						=	 new HtmlTableCell();
			c.Align					=	"center";
			c.Width					=	oneCol ? "100%":"50%";
			Label l					=	new Label();
			l.ID					=	"LblADNVersion0";
			l.Text					=	LocalizeHelper.Localize("ADNCRM_RUNNING_VERSION","Code version");
			l.EnableViewState		=	false;
			c.Controls.Add(l);
			addBr(c);
			l						=	new Label();
			l.ID					=	"LblRunVersion";
            if ( VersionDebug )
			    l.Text		        =	string.Format("{0}-(Core: {1}, AddonNice: {2})",PortalSettings.ProductVersion,PortalSettings.CoreAssemblyInfo.FileVersion,AddonNiceAssemblyInfo.FileVersion);
			else    l.Text			=	PortalSettings.ProductVersion;
			l.EnableViewState		=	false;
			c.Controls.Add(l);
			rw.Controls.Add(c);
			if ( oneCol )
			{
				tb.Controls.Add(rw);
				rw				=	new HtmlTableRow();
				rw.Style.Add( HtmlTextWriterStyle.Width,"100%");
			}
			c						=	 new HtmlTableCell();
			l					=	new Label();
			l.ID				=	"LblADNVersion1";
			l.Text				=	LocalizeHelper.Localize("ADNCRM_DB_VERSION","DatabaseDB version");
			l.EnableViewState	=	false;
			c.Controls.Add(l);
			addBr(c);
			l					=	new Label();
			l.ID				=	"LblDBVersion";
			l.Text				=	DatabaseDB.DatabaseVersion.ToString();
			l.EnableViewState	=	false;
			c.Controls.Add(l);
			rw.Controls.Add(c);
			tb.Controls.Add(rw);

			// 3rd row
			rw					=	new HtmlTableRow();
			rw.Style.Add( HtmlTextWriterStyle.Width,"100%");
			c					=	 new HtmlTableCell();
			c.Align				=	"center";
			c.Width				=	oneCol ? "100%":"50%";
			l					=	new Label();
			l.ID				=	"currentUILanguage";
			l.Text				=	System.Threading.Thread.CurrentThread.CurrentUICulture.Name;
			l.EnableViewState	=	false;
			c.Controls.Add(l);
			addBr(c);
			l					=	new Label();
			l.ID				=	"currentLanguage";
			l.Text				=	System.Threading.Thread.CurrentThread.CurrentCulture.Name;
			l.EnableViewState	=	false;
			c.Controls.Add(l);
			rw.Controls.Add(c);
			if ( oneCol )
			{
				tb.Controls.Add(rw);
				rw				=	new HtmlTableRow();
				rw.Style.Add( HtmlTextWriterStyle.Width,"100%");
			}
			c						=	 new HtmlTableCell();
			l					=	new Label();
			l.ID				=	"LblADNVersion2";
			l.Text				=	LocalizeHelper.Localize("ADNCRM_LICENSE_INFO","License infos:");
			l.EnableViewState	=	false;
			c.Controls.Add(l);
			addBr(c);
			l					=	new Label();
			l.ID				=	"LblLicense";
			l.Text				=	portalSettings.LicenseInfo;
			l.EnableViewState	=	false;
			c.Controls.Add(l);
			rw.Controls.Add(c);
			tb.Controls.Add(rw);

			// 4th row
			rw					=	new HtmlTableRow();
			rw.Style.Add( HtmlTextWriterStyle.Width,"100%");
			c					=	 new HtmlTableCell();
			c.Align				=	"center";
			c.Width				=	oneCol ? "100%":"50%";
			l					=	new Label();
			l.ID				=	"LblADNVersion3";
			l.Text				=	LocalizeHelper.Localize("ADNCRM_CRM_INFO","CRM infos");
			l.EnableViewState	=	false;
			c.Controls.Add(l);
			addBr(c);
			l					=	new Label();
			l.ID				=	"LblCrmServer";
            CRMWrapper.CRMWrapperBase w =   PortalSecurity.Wrapper;
			l.Text				=	string.Format("{0} / {1} / {2}",w.CrmServiceUrl,w.OrganizationName,w.LanguageCode);
			l.EnableViewState	=	false;
			c.Controls.Add(l);
			rw.Controls.Add(c);
			if ( oneCol )
			{
				tb.Controls.Add(rw);
				rw				=	new HtmlTableRow();
				rw.Style.Add( HtmlTextWriterStyle.Width,"100%");
			}
			c						=	 new HtmlTableCell();
			l					=	new Label();
			l.ID				=	"LblADNVersion5";
			l.Text				=	LocalizeHelper.Localize("ADNCRM_NB_USERS","Nb users:");
			l.EnableViewState	=	false;
			c.Controls.Add(l);
			addBr(c);
			l					=	new Label();
			l.ID				=	"LblNbUsers";
			l.EnableViewState	=	false;
            try
            {
                int userCnt     =   PortalSecurity.Wrapper.ActiveUsersCount;
                string userNm   =   PortalSecurity.Wrapper.userName;
                l.Text          =   string.Format("{0} / {1}", userCnt, userNm);
            }
            catch(Exception ex)
            {
                if ( ModuleTraceSwitch.Sw.TraceError )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ADNCRMVersion Page_Load error reading usercount && username ex: {0}",ex),ModuleTraceSwitch.Sw.Info);
            }
			c.Controls.Add(l);
			rw.Controls.Add(c);
			tb.Controls.Add(rw);
			// second image on bottom id only one col
			if ( oneCol )
				tb.Controls.Add(rw1);

   			// Listeners
            if (VersionDebug)
            {
                rw          =   new HtmlTableRow();
                rw.Style.Add(HtmlTextWriterStyle.Width, "100%");
                c           =   new HtmlTableCell();
                c.Align     =   "center";
                c.Width     =   oneCol ? "100%" : "50%";
                c.ColSpan   =   oneCol ? 1 : 2;

                l           =   new Label();
                l.ID        =   "LblADNVersion6";
                l.Text      =   "Listeners";
                l.EnableViewState = false;
                c.Controls.Add(l);
                addBr(c);
                l           =   new Label();
                l.ID        =   "Lblisteners";
                StringBuilder sb = new StringBuilder();
                foreach (TraceListener t in System.Diagnostics.Trace.Listeners)
                {
                    sb.AppendFormat("{0}<br/>", t.Name);
                    foreach (string atstr in t.Attributes.Keys)
                    {
                        sb.AppendFormat("{0}={1}<br/>", atstr, t.Attributes[atstr]);
                    }
                    sb.Append("<br/>");
                }
                l.Text      =   sb.ToString();
                l.EnableViewState = false;
                c.Controls.Add(l);
                rw.Controls.Add(c);
                tb.Controls.Add(rw);
                // CRM groups
                rw          =   new HtmlTableRow();
                rw.Style.Add(HtmlTextWriterStyle.Width, "100%");
                c           =   new HtmlTableCell();
                c.Align     =   "center";
                c.Width     =   oneCol ? "100%" : "50%";

                l           =   new Label();
                l.ID        =   "LblSqlAccessGroup";
                l.Text      =   "SqlAccessGroup";
                l.EnableViewState = false;
                c.Controls.Add(l);
                addBr(c);
                l           =   new Label();
                l.ID        =   "LbSqlAccessGroupName";
                l.Text      =   w.SqlAccessGroupName;
                l.EnableViewState = false;
                c.Controls.Add(l);
                rw.Controls.Add(c);
			    if ( oneCol )
			    {
				    tb.Controls.Add(rw);
				    rw				=	new HtmlTableRow();
				    rw.Style.Add( HtmlTextWriterStyle.Width,"100%");
			    }
                c           =   new HtmlTableCell();
                c.Align     =   "center";
                c.Width     =   oneCol ? "100%" : "50%";

                l           =   new Label();
                l.ID        =   "LblReportingGroup";
                l.Text      =   "ReportingGroupName";
                l.EnableViewState = false;
                c.Controls.Add(l);
                addBr(c);
                l           =   new Label();
                l.ID        =   "LbReportingGroupName";
                l.Text      =   w.ReportingGroupName;
                l.EnableViewState = false;
                c.Controls.Add(l);
                rw.Controls.Add(c);
                tb.Controls.Add(rw);
            }
            PlaceHolder1.Controls.Add(tb);
		}


		public override Guid GuidID 
		{
			get
			{
				return new Guid("{72C6F60A-50C4-4f20-8F89-3E8A27820557}");
			}
		}

		#region Web Form Designer generated code
		/// <summary>
		/// Raises Init event
		/// </summary>
		/// <param name="e"></param>
		override protected void OnInit(EventArgs e)
		{
			InitializeComponent();
			base.OnInit(e);
		}

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent() 
		{    
			this.Load += new System.EventHandler(this.ADNCRMVersion_Load);

		}
		#endregion
	}
}